<template>
  <div class="dv-border-box-12" ref="borderBox12">
    <svg class="dv-border-svg-container" :width="width" :height="height">
      <defs>
        <filter :id="filterId" height="150%" width="150%" x="-25%" y="-25%">
          <feMorphology operator="dilate" radius="1" in="SourceAlpha" result="thicken" />
          <feGaussianBlur in="thicken" stdDeviation="2" result="blurred" />
          <feFlood :flood-color="fade(mergedColor[1] || defaultColor[1], 70)" result="glowColor">
            <animate
              attributeName="flood-color"
              :values="`
                ${fade(mergedColor[1] || defaultColor[1], 70)};
                ${fade(mergedColor[1] || defaultColor[1], 30)};
                ${fade(mergedColor[1] || defaultColor[1], 70)};
              `"
              dur="3s"
              begin="0s"
              repeatCount="indefinite"
            />
          </feFlood>
          <feComposite in="glowColor" in2="blurred" operator="in" result="softGlowColored" />
          <feMerge>
            <feMergeNode in="softGlowColored"/>
            <feMergeNode in="SourceGraphic"/>
          </feMerge>
        </filter>
      </defs>

      <path
        v-if="width && height"
        :fill="backgroundColor"
        stroke-width="2"
        :stroke="mergedColor[0]"
        :d="`
          M15 5 L ${width - 15} 5 Q ${width - 5} 5, ${width - 5} 15
          L ${width - 5} ${height - 15} Q ${width - 5} ${height - 5}, ${width - 15} ${height - 5}
          L 15, ${height - 5} Q 5 ${height - 5} 5 ${height - 15} L 5 15
          Q 5 5 15 5
        `"
      />

      <path
        stroke-width="2"
        fill="transparent"
        stroke-linecap="round"
        :filter="`url(#${filterId})`"
        :stroke="mergedColor[1]"
        :d="`M 20 5 L 15 5 Q 5 5 5 15 L 5 20`"
      />

      <path
        stroke-width="2"
        fill="transparent"
        stroke-linecap="round"
        :filter="`url(#${filterId})`"
        :stroke="mergedColor[1]"
        :d="`M ${width - 20} 5 L ${width - 15} 5 Q ${width - 5} 5 ${width - 5} 15 L ${width - 5} 20`"
      />

      <path
        stroke-width="2"
        fill="transparent"
        stroke-linecap="round"
        :filter="`url(#${filterId})`"
        :stroke="mergedColor[1]"
        :d="`
          M ${width - 20} ${height - 5} L ${width - 15} ${height - 5}
          Q ${width - 5} ${height - 5} ${width - 5} ${height - 15}
          L ${width - 5} ${height - 20}
        `"
      />

      <path
        stroke-width="2"
        fill="transparent"
        stroke-linecap="round"
        :filter="`url(#${filterId})`"
        :stroke="mergedColor[1]"
        :d="`
          M 20 ${height - 5} L 15 ${height - 5}
          Q 5 ${height - 5} 5 ${height - 15}
          L 5 ${height - 20}
        `"
      />
    </svg>

    <div class="border-box-content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
  import autoResize from "../../../../mixins/autoResize";
  import { cloneDeep, debounce, mergeDeep, UUID2 } from "../../../../utils/utils";
  import { fade } from "../../../../utils/datav-color";
  import { onMounted, ref, watch } from "vue";

  export default {
    name: "DvBorderBox12",
    mixins: [autoResize],
    props: {
      color: {
        type: Array,
        default: () => ([])
      },
      backgroundColor: {
        type: String,
        default: 'transparent'
      }
    },
    setup(props) {
      const id = UUID2(32);
      const borderBox12 = ref(null);
      const defaultColor = ref(['#2e6099', '#7ce7fd']);
      const filterId = ref(`border-box-12-filterId-${id}`);
      const mergedColor = ref([]);
      const width = ref(300);
      const height = ref(300);
      const mergeColor = () => {
        mergedColor.value = mergeDeep(cloneDeep(defaultColor.value), props.color || []);
      };
      watch(() => props.color, s => {
        mergeColor();
      });
      onMounted(() => {
        const ro = new ResizeObserver(entries => {
          handleResize(entries);
        });
        ro.observe(borderBox12.value);
        mergeColor();
      });
      const handleResize = debounce((entries) => {
        width.value = entries[0].contentRect.width;
        height.value = entries[0].contentRect.height;
      }, 100);
      return { width, height, borderBox12, mergedColor, mergeColor, handleResize, defaultColor, filterId, fade };
    },
  };
</script>

<style scoped>
  .dv-border-box-12 {
    position: relative;
    width: 100%;
    height: 100%;
  }

  .dv-border-box-12 .dv-border-svg-container {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
  }

  .dv-border-box-12 .border-box-content {
    position: relative;
    width: 100%;
    height: 100%;
  }
</style>
